package cn.wuqingna.legendgem.commands

import cn.wuqingna.legendgem.cache.PlayerManager
import cn.wuqingna.legendgem.gem.GemManager
import cn.wuqingna.legendgem.gem.GemPlant
import cn.wuqingna.legendgem.gui.Gui
import cn.wuqingna.legendgem.loader.ConditionsLoader
import cn.wuqingna.legendgem.loader.GemLoader
import cn.wuqingna.legendgem.loader.GuiLoader
import cn.wuqingna.legendgem.loader.SuitLoader
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.CommandBody
import taboolib.common.platform.command.CommandHeader
import taboolib.common.platform.command.mainCommand
import taboolib.common.platform.command.subCommand
import taboolib.common.platform.function.adaptPlayer
import taboolib.expansion.createHelper
import taboolib.module.chat.Components
import taboolib.module.chat.colored

@CommandHeader(name = "legendgem", aliases = ["lg"])
object CommandMain {
    @CommandBody
    val main = mainCommand {
        createHelper()
    }

    @CommandBody(permission = "legendgem.source")
    val source = subCommand {
        execute<Player> { sender, _, _ ->
            val cache = PlayerManager.get(sender.uniqueId)
            cache.formatAttribute().forEach { (source, attribute) ->
                Components
                    .text("  &7属性源: &a$source".colored())
                    .append("  &6[鼠标移动到这里查看属性]".colored())
                    .hoverText(attribute.attributes)
                    .append("  &a${attribute.runningTime}/ms".colored())
                    .sendTo(adaptPlayer(sender))
            }

        }
    }
    @CommandBody(permission = "legendgem.reload")
    val reload = subCommand {
        execute<ProxyCommandSender> { sender, _, _ ->
            ConditionsLoader.loader()
            GemLoader.init()
            GuiLoader.init()
            SuitLoader.init()
            sender.sendMessage("重载成功")
        }
    }
    @CommandBody(permission = "legendgem.item")
    val item = subCommand {
        dynamic("get") {
            dynamic("args") {
                execute<Player> { sender, context, _: String ->
                    val itemKey = context["args"]
                    val gem = GemManager.getGem(itemKey)
                    sender.inventory.addItem(GemPlant.buildGem(sender, gem))
                }
                dynamic("amount", optional = true) {
                    execute<Player> { sender, context, _: String ->
                        val itemKey = context["args"]
                        val gem = GemManager.getGem(itemKey)
                        val item = GemPlant.buildGem(sender, gem)
                        item.amount = context["amount"].toInt()
                        sender.inventory.addItem(item)
                    }
                }
            }
        }
    }
    @CommandBody(permission = "legendgem.opengui")
    val opengui = subCommand {
        dynamic("args") {
            suggestion<Player> { _, _ ->
                GuiLoader.guis.keys.toList()
            }
            execute<Player> { sender, context, _: String ->
                val gui = Gui(GuiLoader.guis[context["args"]]!!)
                gui.open(sender)
            }
        }
        dynamic("player name", optional = true) {
            suggestion<Player> { _, _ ->
                Bukkit.getOnlinePlayers().map { it.name }
            }
            dynamic("args") {
                suggestion<Player> { _, _ ->
                    GuiLoader.guis.keys.toList()
                }
                execute<Player> { _, context, _: String ->
                    val gui = Gui(GuiLoader.guis[context["args"]]!!)
                    gui.open(Bukkit.getPlayer(context["player name"]))

                }
            }
        }
    }
}